package com.example.myapplication

import android.net.Uri
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import androidx.databinding.DataBindingUtil
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import com.example.myapplication.databinding.ActivityMainBinding
import com.example.myapplication.vm.ClickListenner
import com.example.myapplication.vm.MyVm
import com.example.myapplication.vm.ObserverData
import com.example.myapplication.vm.Person
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.android.synthetic.main.activity_main.view.*
import kotlinx.android.synthetic.main.activity_main.view.imageView

class MainActivity : AppCompatActivity(), ClickListenner {

    private lateinit var binding: ActivityMainBinding
    private lateinit var view:View
    private lateinit var myVm:MyVm

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
//        setContentView(R.layout.activity_main)

        binding = DataBindingUtil.setContentView(this, R.layout.activity_main)
        view = binding.root
        binding.person = Person("lzz", 25)
        binding.lister = this
        binding.lifecycleOwner = this

        myVm = ViewModelProvider(this)[MyVm::class.java]

        myVm.name.observe(this, Observer {
            this.view.textView.text =  ""+myVm.id.value
            this.view.textView2.text =  ""+myVm.name.value
        })

        binding.observerData = ObserverData()
        binding.observerData?.age?.set(1)
        binding.observerData?.name?.set("123")
        binding.observerData?.imgUrl?.
        set("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1600623172136&di=ed0c11d3792512aed6b332c00e276bfb&imgtype=0&src=http%3A%2F%2F00imgmini.eastday.com%2Fmobile%2F20181029%2F20181029151038_af31c3f0b09987f96521c80a69b0afd9_2.jpeg")

        view.button4.setOnClickListener(View.OnClickListener(){
            binding.observerData?.name?.set(binding.observerData?.name?.get()+"8,")
            binding.observerData?.imgUrl?.set("https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=2283247857,1974700361&fm=26&gp=0.jpg")

        });

    }

    override fun onClickButton(view: View) {
        binding.person?.age = 18
        binding.person?.name = "lzz123564"
//        this.view.textView.text = "18"

        myVm.id.value = 22
        myVm.name.value = "haofsdlflk"

        binding.observerData?.imgUrl?.set("https://s1s1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=1796361926,2022556443&fm=26&gp=0.jpg")

    }


}